package com.scedu.service.impl;


import com.scedu.dao.UserDao;
import com.scedu.domain.SysRole;
import com.scedu.domain.SysUser;
import com.scedu.service.RoleService;
import com.scedu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class UserServiceImpl  implements UserService {
    @Autowired
    private UserDao userDao;

    @Autowired
    private RoleService roleService;
//    @Autowired
//    BCryptPasswordEncoder bCryptPasswordEncoder;

    @Override
    public void save(SysUser user) {
        BCryptPasswordEncoder bCryptPasswordEncoder=new BCryptPasswordEncoder();
        user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));//修改用户添加时的密码为加密后的密码
        userDao.save(user);
    }

    @Override
    public List<SysUser> findAll() {
        return userDao.findAll();
    }

    @Override
    public Map<String, Object> toAddRolePage(Integer id) {
        List<SysRole> allRoles = roleService.findAll();
        List<Integer> myRoles = userDao.findRolesByUid(id);
        Map<String, Object> map = new HashMap<>();
        map.put("allRoles", allRoles);
        map.put("myRoles", myRoles);
        return map;
    }

    @Override
    public void addRoleToUser(Integer userId, Integer[] ids) {
        userDao.removeRoles(userId);
        for (Integer rid : ids) {
            userDao.addRoles(userId, rid);
        }
    }
    /*
    认证业务
        思路:1、根据用户名查询数据库中的用户对象，2、转换为UserDetails类型对象
         String username 用户在浏览器输入的用户名
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        try {
            //1、根据用户名查询数据库中的用户对象
            SysUser sysUser = userDao.findByName(username);
            if(sysUser==null){
                //登录失败
                return null;
            }
            //2、转换为UserDetails类型对象

            //{noop}后面的密码，springsecurity会认为是原文

            String password=sysUser.getPassword(); //密码 加密认证密码
            //Collection<? extends GrantedAuthority> authorities = null; //角色列表
            List<SimpleGrantedAuthority> authorities = new ArrayList<>(); //角色列表
            //获取该用户在数据库中的角色
            List<SysRole> roles = sysUser.getRoles();
            for (SysRole role : roles) {
                authorities.add(new SimpleGrantedAuthority(role.getRoleName()));//给用户添加角色
            }
            // authorities.add(new SimpleGrantedAuthority("ROLE_USER")); //给用户添加角色ROLE_USER
            UserDetails userDetails=new User(username,password,
                    sysUser.getStatus()==1,
                    true,true,true,
                    authorities); //用户名，密码，是否有效，角色列表
            return userDetails;
        }catch (Exception e){
            //认证失败
            return null;
        }


    }
}
